﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WiX_Standardizer.Common.SettingsParser;

namespace WiX_Standardizer.Common.Elements
{
	public static class Package
	{
		public static XElement Create()
		{
			XElement package = new XElement(SettingsParser.General.GetWixNamespace() + "Package");

			string description = SettingsParser.Package.GetProductName() + " Setup";
			string manufacturer = SettingsParser.Package.GetManufacturer();
			string installerVersion = SettingsParser.Package.GetInstallerVersion();
			string compressed = SettingsParser.Package.GetCompressed();

			package.Add(new XAttribute("Description", description));
			package.Add(new XAttribute("Comments", package.Attribute("Description").Value));
			package.Add(new XAttribute("Manufacturer", manufacturer));
			package.Add(new XAttribute("InstallerVersion", installerVersion));
			package.Add(new XAttribute("Compressed", compressed));

			return package;
		}

		public static XElement Correct(XElement startingPoint)
		{
			XElement package = startingPoint;

			// Check if the Manufacturer attribute exists.  If not create it.  
			// If so make sure the value is set to the proper value
			string manufacturer = SettingsParser.Package.GetManufacturer();
			if (!package.Attributes("Manufacturer").Any())
				package.Add(new XAttribute("Manufacturer", manufacturer));
			else if (!package.Attribute("Manufacturer").Value.Equals(manufacturer))
				package.SetAttributeValue("Manufacturer", manufacturer);

			// Check if the InstallerVersion attribute exists.  If not create it.  
			// If so make sure the value is set to the proper value.
			// "405" means that Windows Installer Version 4.5 is required for installation
			string installerVersion = SettingsParser.Package.GetInstallerVersion();
			if (!package.Attributes("InstallerVersion").Any())
				package.Add(new XAttribute("InstallerVersion", installerVersion));
			else if (!package.Attribute("InstallerVersion").Value.Equals(installerVersion))
				package.SetAttributeValue("InstallerVersion", installerVersion);

			// Check if the Compressed attribute exists.  If not create it.  
			// If so make sure the value is set to the proper value
			string compressed = SettingsParser.Package.GetCompressed();
			if (!package.Attributes("Compressed").Any())
				package.Add(new XAttribute("Compressed", "yes"));
			else if (!package.Attribute("Compressed").Value.Equals("yes"))
				package.SetAttributeValue("Compressed", "yes");

			// Check if the Description attribute exists.  If not create it.  
			// If so make sure the value is set to the proper value.
			string description = SettingsParser.Package.GetProductName();
			description += " Setup";
			if (!package.Attributes("Description").Any())
				package.Add(new XAttribute("Description", description));
			else if (!package.Attribute("Description").Value.Equals(description))
				package.SetAttributeValue("Description", description);

			return package;
		}
	}
}
